package com.ittest.controller;

import com.ittest.common.Code;
import com.ittest.common.Result;
import com.ittest.pojo.Dept;
import com.ittest.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门表现层
 */
@RestController
@RequestMapping("/dept")
public class DeptController {

    // 注入service
    @Autowired
    private DeptService deptService;


    /**
     * 新增部门
     * @param dept
     * @return
     */
    @PostMapping
    public Result<Dept> add(@RequestBody Dept dept){
        boolean result = deptService.addDept(dept);
        if (result){
            return new Result<Dept>(Code.OK,"新增成功！");
        }else{
            return new Result<Dept>(Code.ERROR,"新增失败！");
        }
    }

    /**
     * 删除部门
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result<Dept> delete(@PathVariable("id") Integer id){
        boolean result = deptService.deleteDept(id);
        if (result){
            return new Result<Dept>(Code.OK,"部门删除，成功！");
        }else{
            return new Result<Dept>(Code.ERROR,"部门删除，失败！");
        }
    }

    /**
     * 修改部门名称
     */
    @PutMapping
    public Result<Dept> update(@RequestBody Dept dept){
        boolean result = deptService.updateDept(dept);
        if (result){
            return new Result<Dept>(Code.OK,"修改部门信息，成功！");
        }else{
            return new Result<Dept>(Code.ERROR,"修改部门信息，失败！");
        }
    }

    /**
     * 查询所有部门信息
     * @return
     */
    // 部门不多，无需分页
    @GetMapping
    public Result<Dept> findAll(){
        List<Dept> list = deptService.findAll();
        return new Result(Code.OK,"分页查询所有部门，成功",list);
    }
}
